﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using AlertSystem.Business;
using System.Web.UI.HtmlControls;
using Utils;
using System.IO;
using System.Data;
namespace AlertSystem.Web
{
    public partial class Warning_SystemManagement : BasePage
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            pagingtop.PagingChange += new EventHandler(paging_PagingChange);
            pagingbottom.PagingChange += new EventHandler(paging_PagingChange);
        }

        private void paging_PagingChange(object sender, EventArgs e)
        {
            pagingtop.PagingChange -= new EventHandler(paging_PagingChange);
            pagingbottom.PagingChange -= new EventHandler(paging_PagingChange);
            pagingtop.CurrentPage = (sender as UserControl.ucPaging).CurrentPage;
            pagingbottom.CurrentPage = (sender as UserControl.ucPaging).CurrentPage;
            BindGridData();
            pagingtop.Visible = pagingbottom.Visible = (sender as UserControl.ucPaging).TotalPage > 1;
            pagingtop.PagingChange += new EventHandler(paging_PagingChange);
            pagingbottom.PagingChange += new EventHandler(paging_PagingChange);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PageSize = Convert.ToInt32(tSortable_length.SelectedValue);
                pagingtop.CurrentPage = pagingbottom.CurrentPage = 1;
                pagingtop.PageSize = pagingbottom.PageSize = PageSize;
                pagingtop.PageDisplay = pagingbottom.PageDisplay = 6;
                BindDropDownData();
                BindGridData();
            }
        }

        private void BindGridData()
        {
            try
            {
                int currentPage = pagingtop.CurrentPage;
                int pageSize = pagingtop.PageSize;
                int totalRecord = 0;
                grvData.DataSource = Warning_System.Paging(DB, currentPage, pageSize, ref totalRecord, txtSearchSystemID.Text.Trim(), txtSearchDescription.Text.Trim());
                pagingtop.TotalRecord = pagingbottom.TotalRecord = totalRecord;
                pagingtop.Visible = pagingbottom.Visible = pagingtop.TotalPage > 1;
                grvData.DataBind();
                ltrInfo.Text = "Tổng cộng: " + totalRecord;
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
        }

        private void BindDropDownData()
        {

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            pagingtop.PagingChange -= new EventHandler(paging_PagingChange);
            pagingbottom.PagingChange -= new EventHandler(paging_PagingChange);
            pagingtop.CurrentPage = pagingbottom.CurrentPage = 1;
            pagingtop.PagingChange += new EventHandler(paging_PagingChange);
            pagingbottom.PagingChange += new EventHandler(paging_PagingChange);
            BindGridData();
        }

        protected void tSortable_length_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                PageSize = Convert.ToInt32(tSortable_length.SelectedValue);
                pagingtop.PageSize = pagingbottom.PageSize = PageSize;
                BindGridData();
            }
            catch { }
        }

        protected void grvData_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                string ID = e.CommandArgument.ToString();
                hdfID.Value = ID;
                switch (e.CommandName)
                {
                    case "Editing":
                        hdfAction.Value = "Editing";
                        Warning_System editObj = DB.Warning_Systems.FirstOrDefault(p => p.System_Id == ID);
                        if (editObj != null)
                        {
                            txtSystemID.Text = editObj.System_Id;
                            txtSystemID.Enabled = false;
                            txtDescription.Text = editObj.Description;
                            RegisterStartUpScript("OpenDialog('dialog1',600);");
                        }
                        break;
                    case "Deleting":
                        Authorize(Role.Write);
                        Warning_System deleteObj = DB.Warning_Systems.FirstOrDefault(p => p.System_Id == ID);
                        if (deleteObj != null)
                        {
                            DB.Warning_Systems.DeleteOnSubmit(deleteObj);
                            DB.SubmitChanges();
                            InsertChangeLog("Xóa dữ liệu Warning_System");
                            BindGridData();
                        }
                        break;
                }
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                Authorize(Role.Write);
                if (ValidateInfo())
                {
                    Warning_System obj;
                    if (hdfAction.Value == "Editing")
                    {
                        obj = DB.Warning_Systems.FirstOrDefault(p => p.System_Id == hdfID.Value);
                    }
                    else
                    {
                        obj = new Warning_System();
                    }
                    if (obj != null)
                    {
                        //Gán thuộc tính
                        obj.System_Id = txtSystemID.Text.Trim();
                        obj.Description = txtDescription.Text.Trim();
                        if (hdfAction.Value == "AddNew")
                        {
                            if (ValidateInfo(obj))
                            {
                                DB.Warning_Systems.InsertOnSubmit(obj);
                            }
                            else
                            {
                                RegisterStartUpScript("OpenDialog('dialog1',600);");
                                return;
                            }
                        }
                        //save
                        DB.SubmitChanges();
                        //Ghi log tương tác
                        InsertChangeLog(string.Format((hdfAction.Value == "Editing" ? "Cập nhật" : "Thêm mới") + " dữ liệu Warning_System, System_Id: {0}", obj.System_Id));
                    }
                }
                BindGridData();
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
        }

        protected override bool ValidateInfo(object entity)
        {
            Warning_System obj = DB.Warning_Systems.FirstOrDefault(p => string.Compare(((Warning_System)entity).System_Id, p.System_Id, true) == 0);
            if (obj != null)
            {
                lbError.Text = "System_Id đã tồn tại trong hệ thống.";
                return false;
            }
            return base.ValidateInfo(entity);
        }
    }
}